package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SearchActivity extends AppCompatActivity {
    EditText searchtext;
    Button searchbtn;
    Spinner type;
    Spinner order;

    UserDAO dao;
    List<Thing> thingListQueryRes;
    List<Thing> thingListShow;
    ListView listView;
    Button back;
    ThingAdapter adapter;
    List<String>typelist;
    ArrayAdapter typeadapter;
    ArrayAdapter orderadapter;
    Set<Thing> tmpThingSet;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initpar();
    }

    private void initpar() {
        dao=new UserDAO(this);
        thingListQueryRes =new ArrayList<>();
        thingListShow=new ArrayList<>();
        typelist=new ArrayList<>();
        tmpThingSet=new HashSet<>();
        typeadapter=new ArrayAdapter(this,androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,typelist);

        searchtext=findViewById(R.id.text_searchname);
        searchbtn=findViewById(R.id.btn_search);
        type=findViewById(R.id.search_type1);
        type.setAdapter(typeadapter);
        type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                updateShowList();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        order =findViewById(R.id.search_dateorder);
        String[]orderlist=new String[]{"按修改日期降序","按修改日期升序","按创建日期降序","按创建日期升序"};
        orderadapter =new ArrayAdapter(this,androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,orderlist);
        order.setAdapter(orderadapter);
        order.setSelection(0);
        order.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                updateShowList();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        listView=findViewById(R.id.itemlist);
        back=findViewById(R.id.btn_backtomainactivity);

        adapter=new ThingAdapter(SearchActivity.this,R.layout.thinglabel, thingListShow);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int thingid= thingListShow.get(i).getId();
                Intent intent=new Intent(SearchActivity.this, AddAndEditActivity.class);
                Bundle b=new Bundle();
                b.putSerializable("data",(Serializable) thingid);
                intent.putExtras(b);
                startActivity(intent);
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchNow();
            }
        });
        updateTypelist();
    }

    private void searchNow() {
        tmpThingSet.clear();
        tmpThingSet.addAll(dao.queryThingByName(searchtext.getText().toString()));
        tmpThingSet.addAll(dao.queryThingByInformation(searchtext.getText().toString()));

        thingListQueryRes.clear();
        thingListQueryRes.addAll(tmpThingSet);
        Log.d("输出", "searchNow: "+searchtext.getText().toString()+" "+thingListQueryRes.size());
        updateShowList();
    }

    private void updateShowList() {
        Log.d("输出", "updateShowList: ");
        thingListShow.clear();
        int tag=type.getSelectedItemPosition();
        if (thingListQueryRes==null||thingListQueryRes.size()==0){
            Log.d("输出", "updateShowList: queryres = null或size=0");
            return;
        }
        if(tag==0){
            thingListShow.addAll(thingListQueryRes);
        }else {
            for (int i = 0; i < thingListQueryRes.size(); i++) {
                if (thingListQueryRes.get(i).getTypeFirst()==tag){
                    thingListShow.add(thingListQueryRes.get(i));
                }
            }
        }
        Log.d("输出", "updateShowList: "+thingListShow.size());
        //排序，未完成
        orderShowThingList();
        if(adapter!=null){
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
        }
    }

    private void orderShowThingList() {
        if (!thingListShow.isEmpty()){
            switch (order.getSelectedItemPosition()){
                case 0:{
                    thingListShow.sort(new Comparator<Thing>() {
                        @Override
                        public int compare(Thing thing, Thing t1) {
                            if (thing.getDatetime()>t1.getDatetime())return -1;
                            else if (thing.getDatetime()<t1.getDatetime())return 1;
                            else return thing.getId()-t1.getId();
                        }
                    });
                    break;
                }
                case 1:{
                    thingListShow.sort(new Comparator<Thing>() {
                        @Override
                        public int compare(Thing thing, Thing t1) {
                            if (thing.getDatetime()>t1.getDatetime())return 1;
                            else if (thing.getDatetime()<t1.getDatetime())return -1;
                            else return thing.getId()-t1.getId();
                        }
                    });
                    break;
                }
                case 2:{
                    thingListShow.sort(new Comparator<Thing>() {
                        @Override
                        public int compare(Thing thing, Thing t1) {
                            return t1.getId()-thing.getId();
                        }
                    });
                    break;
                }
                case 3:{
                    thingListShow.sort(new Comparator<Thing>() {
                        @Override
                        public int compare(Thing thing, Thing t1) {
                            return thing.getId()-t1.getId();
                        }
                    });
                    break;
                }
            }
        }
    }

    @Override
    protected void onResume() {
        //如果搜罗栏不为空，则立即进行一次搜索
        super.onResume();
        //if (!searchtext.getText().toString().isEmpty()) {
            searchNow();
        //}
        updateTypelist();
        updateShowList();
    }
    private void updateTypelist() {
        typelist.clear();
        typelist.add("全部");
        typelist.addAll(dao.queryType1All());
        if (typeadapter!=null){
            typeadapter.notifyDataSetChanged();
            type.setSelection(0);
        }
    }
}
